#include "tcp_menu.h"

TCP_Menu::TCP_Menu(QWidget *parent) : QMainWindow(parent)
{
    Sysctrl->SYS_CTL_MODE =0;
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(640,430);
    timer = new QTimer(this);
    timer->start(1000);
    //修改数据时闪烁的间隔
    timer2 = new QTimer(this);
    timer2->setInterval(250);
    UI_Init();
    connect_Init();
}

void TCP_Menu::UI_Init()
{

    the_btn_style[0]=("\
                      outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                      background-color: rgb(0, 170, 255);	    /* 背景颜色 */\
                      border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                      border-style: outset;\
                      /* 按钮背景图标设置 */\
                      background-image: url(:/pic/access/气象站.png); /* 背景图片 */\
                      background-origin: content;\
                      background-position: left;	         /* 背景图片的位置 */\
                      padding-right: 0px;	    /*内边距设置*/	    \
                      padding-left: 10px;		/*  */	\
                      padding-bottom: 10px;		/*  */    \
                      padding-top: 10px;		/*  */    \
                      background-repeat: no-repeat;	         /* 设置背景图像的平铺模式 */\
                      /* 按钮文本设置 */\
                      text-align: content;			             /* 文本的位置 */\
                      font-size: 18px;\
                      color: #FFFFFF;			                 /* 文本颜色 */\
                 "
);

//
the_btn_style[2]=("QPushButton:pressed{background-color:rgb(99,211,133);}");
the_btn_style[1]=("\
                  outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                  border-radius: 8px;		                 /* 按钮边框的圆角设置 */\
                  border-style: outset;\
                  /* 按钮背景图标设置 */\
                  background-image: url(:/pic/Airflow_mode/等待.png); /* 背景图片 */\
                  background-origin: content;\
                  background-position: left;	         /* 背景图片的位置 */\
                  padding-right: 0px;	    /*内边距设置*/	    \
                  padding-left: 10px;		/*  */	\
                  padding-bottom: 10px;	/*  */   \
                  padding-top:10px;		/*  */    \
                  background-repeat: repeat;	         /* 设置背景图像的平铺模式 */\
                  /* 按钮文本设置 */\
                  text-align:content ;			             /* 文本的位置 */\
                  font-size: 22px;\
                  color: rgb(225, 225, 225);			                 /* 文本颜色 */\
                  ");

                  for(int i=0;i<7;i++)//创建主屏幕显示的按键
                  {
                       theBtn[i] = new QPushButton(this);
              //        newbutton->setGeometry(30+(i*150),260,120,90 );//设定每隔放置一个按键
                      theBtn[i]->setFixedSize(150,40);
                      theBtn[i]->geometry();
                      btnlist_ui.append(theBtn[i]);
                  }
                  theBtn[0]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/GESYTEC系列号.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/GESYTEC系列号.png); /* 背景图片 */"+"}");
                  theBtn[1]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/TCP IP设置.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/TCP IP设置.png); /* 背景图片 */"+"}");
                  theBtn[2]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/仪器识别号.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/仪器识别号.png); /* 背景图片 */"+"}");
                  theBtn[3]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/数据流配置.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/数据流配置.png); /* 背景图片 */"+"}");
                  theBtn[4]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/通讯协议.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/通讯协议.png); /* 背景图片 */"+"}");
                  theBtn[5]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/串口设置.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/串口设置.png); /* 背景图片 */"+"}");
                  theBtn[6]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/返回.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/返回2.png); /* 背景图片 */"+"}");

btnlist_ui.at(0)->setText(tr("     IP地址"));
btnlist_ui.at(1)->setText(tr("     掩码"));
btnlist_ui.at(2)->setText(tr("     网关"));
btnlist_ui.at(3)->setText(tr("     主机"));
btnlist_ui.at(4)->setText(tr("     IP地址设置"));
btnlist_ui.at(5)->setText(tr("     使用DHCP"));
btnlist_ui.at(6)->setText(tr("     返回"));

for(int i=0;i<9;i++)//创建主屏幕显示的按键
{
     theLabel[i] = new QLabel(this);
     theLabel[i]->setStyleSheet("QLabel{\
                            outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                            border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                            border-style: outset;\
                            /* 按钮文本设置 */\
                            qproperty-alignment: 'AlignCenter| AlignCenter';			             /* 文本的位置 */\
                            font-size: 20px;\
                            color: #123567;			                 /* 文本颜色 */\
                       }"
      );
}
theLabel[0]->setStyleSheet("QLabel{ image: url(:/pic/logo.png); background-color: rgb(20, 170, 225);}"    );
theLabel[0]->setStyleSheet("QLabel{"
                      + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
theLabel[1]->setStyleSheet("QLabel{ image: url(:/pic/左.png); }"    );
theLabel[2]->setStyleSheet("QLabel{ image: url(:/pic/右.png); }"    );

theLabel[0]->setGeometry(0,0,640,60);//设定每隔放置一个按键
theLabel[1]->setGeometry(0,0,300,60);//设定每隔放置一个按键
theLabel[2]->setGeometry(340,0,300,60);//设定每隔放置一个按键
theLabel[0]->setText("串口设置");

switch (Sysset->CommuSet.Serial_Opt.boundrate)
{
case 2400:
{
   flag_bundrate=0;//1->4800 2->9600  3->19200  4->38400 5->57600  6->115200
   break;
}
case 4800:
{
   flag_bundrate=1;
   break;
}

case 9600:
{
   flag_bundrate=2;
   break;
}

case 19200:
{
   flag_bundrate=3;
   break;

}
case 38400:
{
   flag_bundrate=4;
   break;
}
case 57600:
{
   flag_bundrate=5;
   break;
}

case 115200:
{
   flag_bundrate=6;
   break;
}
default:
   break;
}

//IP地址设置
theLabel[3]->setText(Sysset->Tcp_Cfg[0].ip);
//掩码
theLabel[4]->setText(Sysset->Tcp_Cfg[0].mask);
//网关
theLabel[5]->setText(Sysset->Tcp_Cfg[0].gateway);
//主机
         theLabel[6]->setText(QString::number(Sysset->CommuSet.Serial_Opt.stopbit));
//协议
qDebug()<<Sysset->CommuSet.Com_Bus_Sel;
theLabel[7]->setText(Sysset->Tcp_Cfg[0].serverip);
if(Sysset->Tcp_Cfg[0].Is_DHCP==0){
    theLabel[8]->setText("否");
    }
else{
    theLabel[8]->setText("是");
}

//事件过滤器
foreach (QPushButton *btn, btnlist_ui) {
   btn->installEventFilter(this);
}

QGridLayout *theLayout = new QGridLayout();
//                 theLayout->setAlignment(Qt::AlignCenter);
theLayout->addWidget(btnlist_ui.at(0), 0, 0);
theLayout->addWidget(btnlist_ui.at(1), 1, 0);
theLayout->addWidget(btnlist_ui.at(2), 2, 0);
theLayout->addWidget(btnlist_ui.at(3), 3, 0);
theLayout->addWidget(btnlist_ui.at(4), 4, 0);
theLayout->addWidget(btnlist_ui.at(5), 5, 0);
theLayout->addWidget(btnlist_ui.at(6), 6, 0);
theLayout->addWidget(theLabel[3], 0, 2);
theLayout->addWidget(theLabel[4], 1, 2);
theLayout->addWidget(theLabel[5], 2, 2);
theLayout->addWidget(theLabel[6], 3, 2);
theLayout->addWidget(theLabel[7], 4, 2);
theLayout->addWidget(theLabel[8], 5, 2);
theLayout->setContentsMargins(50,90,50,50);//(int left, int top, int right, int bottom)
theLayout->setSizeConstraint(QLayout::SetMinimumSize);
 setLayout(theLayout);

 QWidget* widget = new QWidget(this);
 widget->setGeometry(0,0,640,330);
 widget->setLayout(theLayout);

}
 void TCP_Menu::connect_Init()
 {

     //定时器
     connect(timer,&QTimer::timeout,[=](){

     });

     //timer2
     connect(timer2,&QTimer::timeout,[=](){
            if(show_flag)
            {
                str_show="";
                str_show_3 ="";
                for(int i=0;i<15;i++)
                {
                    if(num_str[i]=='.')
                        str_show_3.append(". ");
                    else
                        str_show_3.append(num_str[i]);
                    str_show.append(num_str[i]);
                }
                Label_show->setText(str_show_3);
                show_flag =0 ;
            }
            else
            {
                str_show_2="";
                for(int i=0;i<15;i++)
                {
                    if(i!=cnt_flag && num_str[i]!='.')
                        str_show_2.append(num_str[i]);
                    else if(i ==cnt_flag || num_str[i]!='.')
                        str_show_2.append("  ");
                    else if(num_str[i]=='.')
                        str_show_2.append(". ");
                }
                Label_show->setText(str_show_2);
                show_flag=1;
            }
        });

     //IP地址设置
     connect(btnlist_ui.at(0),&QPushButton::clicked,[=](){
         Sel=0;
         Label_show = theLabel[3];
         if(!Sysset->Lock_Status)
         {
         if(change_flag) //切换至按键控制模式
                {
                    change_flag = 0;
                    timer2->stop();
                    Label_show->setText(str_show);
                    change_Number(Label_show->text());
                }
                else  if(!change_flag) //切换至数据修改模式
                {
                    FW_cnt(Label_show->text());
                    change_flag = 1;
                    timer2->start();
                }
         }

     });
     //掩码
     connect(btnlist_ui.at(1),&QPushButton::clicked,[=](){
         Sel=1;
         Label_show = theLabel[4];
         if(!Sysset->Lock_Status)
         {
         if(change_flag) //切换至按键控制模式
                {
                    change_flag = 0;
                    timer2->stop();
                    qDebug()<<str_show;
                    Label_show->setText(str_show);
                    change_Number(Label_show->text());
                }
                else  if(!change_flag) //切换至数据修改模式
                {
                    FW_cnt(Label_show->text());
                    change_flag = 1;
                    timer2->start();
                }
         }
     });
     //网关
     connect(btnlist_ui.at(2),&QPushButton::clicked,[=](){
         Sel=2;
         Label_show = theLabel[5];
         if(!Sysset->Lock_Status)
         {
         if(change_flag) //切换至按键控制模式
                {
                    change_flag = 0;
                    timer2->stop();
                    Label_show->setText(str_show);
                    change_Number(Label_show->text());
                }
                else  if(!change_flag) //切换至数据修改模式
                {
                    FW_cnt(Label_show->text());
                    change_flag = 1;
                    timer2->start();
                }
         }
     });
     //主机
     connect(btnlist_ui.at(3),&QPushButton::clicked,[=](){
         Sel=3;
         SoftKeyboard *board = new SoftKeyboard();
         board->show();
         connect(board,&SoftKeyboard::sendText,this,&TCP_Menu::Recive_TCPdata);
             connect(board,&SoftKeyboard::destroyed,[=](){
             btnlist_ui.at(3)->setFocus();
                     });
     });
     //IP地址设置
     connect(btnlist_ui.at(4),&QPushButton::clicked,[=](){
         Sel=5;
         Label_show = theLabel[7];
         if(!Sysset->Lock_Status)
         {
         if(change_flag) //切换至按键控制模式
                {
                    change_flag = 0;
                    timer2->stop();
                    Label_show->setText(str_show);
                    change_Number(Label_show->text());
 //                   Sysset->Tcp_Cfg[0].tcp_change_flag = 1;
                }
                else  if(!change_flag) //切换至数据修改模式
                {
                    FW_cnt(Label_show->text());
                    change_flag = 1;
                    timer2->start();
                }
         }
     });

 //DHCP
     connect(btnlist_ui.at(5),&QPushButton::clicked,[=](){
         if(Sysset->Tcp_Cfg[0].Is_DHCP==0){
             theLabel[8]->setText("是");
             Sysset->Tcp_Cfg[0].Is_DHCP=1;
             }
         else{
             theLabel[8]->setText("否");
             Sysset->Tcp_Cfg[0].Is_DHCP=0;
         }
     });
     //返回按钮
     connect(btnlist_ui.at(6),&QPushButton::clicked,[=](){
         this->parentWidget()->show();
         this->close();
         Sysctrl->Para_Save = 1;
     });

 }
 void TCP_Menu::FW_cnt(QString num)
 {
     if(num.length()<2) //NULL
         num="000.000.000.000";
     int length = num.length();
   int  temp=0;
   int  ip_flag=0;
     //192.168.1.2 -->192.168.001.002
     for(int j=0;j<length;j++)
     {
         if(num.at(j)=='.' && temp==0)
         {
             if(j==1)
                 ip_stand[ip_flag]="00"+num.mid(0,1);
             else if(j==2)
                 ip_stand[ip_flag]="0"+num.mid(0,2);
             else
                 ip_stand[ip_flag]=num.mid(0,j);
             temp=j;
             ip_flag++;
         }
         else if(num.at(j)=='.' && temp!=0)
         {
             if(num.mid(temp+1,j-temp-1).length()==1)
                 ip_stand[ip_flag]="00"+num.mid(temp+1,j-temp-1);
             else if(num.mid(temp+1,j-temp-1).length()==2)
                 ip_stand[ip_flag]="0"+num.mid(temp+1,j-temp-1);
             else
                 ip_stand[ip_flag] = num.mid(temp+1,j-temp-1);
             temp=j;
             ip_flag++;
         }
         if(j==length-1)
         {
             if(num.mid(temp+1,j-temp).length()==1)
                 ip_stand[ip_flag]="00"+num.mid(temp+1,j-temp);
             else if(num.mid(temp+1,j-temp).length()==2)
                 ip_stand[ip_flag]="0"+num.mid(temp+1,j-temp);
             else
                 ip_stand[ip_flag] = num.mid(temp+1,j-temp);
         }
     }
     QString new_IP = ip_stand[0]+"."+ip_stand[1] +"." +ip_stand[2] +"."+ip_stand[3];
     for(int k=0;k<new_IP.length();k++)
         num_str[k]=new_IP.at(k);
 }
 void TCP_Menu::change_Number(QString number)
 {
     int s1 = number.mid(0,3).toInt();
     int s2 = number.mid(4,3).toInt();
     int s3 = number.mid(8,3).toInt();
     int s4 = number.mid(12,3).toInt();
     QString s = QString::number(s1) +"."+QString::number(s2) +"."+QString::number(s3) +"."+QString::number(s4);
//     qDebug()<<s1<<s2<<s3<<s4;
     Recive_TCPdata(s);
 }

 void TCP_Menu::Recive_TCPdata(QString s)
 {
     switch (Sel) {
     case 0:
     {
         qstrcpy(Sysset->Tcp_Cfg[0].ip,s.toLatin1().data());
         theLabel[3]->setText(Sysset->Tcp_Cfg[0].ip);
         s= "ifconfig eth0 " + s +" netmask " +Sysset->Tcp_Cfg[0].mask;
         system(s.toLatin1().data());
         break;
     }
     case 1:
     {
         qstrcpy(Sysset->Tcp_Cfg[0].mask,s.toLatin1().data());
         theLabel[4]->setText(Sysset->Tcp_Cfg[0].mask);
         s="ifconfig eth0 " + theLabel[3]->text()+" netmask " +s;
         system(s.toLatin1().data());
         break;
     }
 case 2:
     {
         qstrcpy(Sysset->Tcp_Cfg[0].gateway,s.toLatin1().data());
         theLabel[5]->setText(Sysset->Tcp_Cfg[0].gateway);
         break;
     }
 case 3:
     {
         qstrcpy(Sysset->Tcp_Cfg[0].Host_Name,s.toLatin1().data());
         theLabel[6]->setText(s);
         break;
     }
 case 4:
     {
         qstrcpy(Sysset->Tcp_Cfg[0].NTPserverip,s.toLatin1().data());
//         ui->label_NTP->setText(Sysset->Tcp_Cfg[0].NTPserverip);
         break;
     }
     case 5:
         {
             qstrcpy(Sysset->Tcp_Cfg[0].serverip,s.toLatin1().data());
    theLabel[7]->setText(Sysset->Tcp_Cfg[0].serverip);
             break;
         }
     default:
         break;
     }
     Sysctrl->Para_Save = 1;
 }

 bool TCP_Menu::eventFilter(QObject *watched, QEvent *event)
     {

             for(int i=0;i<btnlist_ui.length();i++)
                 if(btnlist_ui.at(i) == watched)
             {
                 if(event->type() == QEvent::KeyPress)
                 {
                    QKeyEvent *ev = (QKeyEvent *)event;
                    QPushButton *button = (QPushButton *)watched;
                    switch (ev->key())
                    {
                    case OK:
                      {
                        emit  button->clicked();
                        break;
                      }
                    case UP://上
                    {
                        if(change_flag && cnt_flag!=3&&cnt_flag!=7&&cnt_flag!=11)
                        {
                            unsigned short temp = num_str[cnt_flag].unicode();
                            if(num_str[cnt_flag] == '9')
                                num_str[cnt_flag]='0';
                            else
                            {
                                temp++;
                                num_str[cnt_flag] = temp;
                            }
                        }
                        else if((!change_flag)&&(focusWidget() != btnlist_ui.first()))
                             focusPreviousChild();
                        break;
                    }
                    case DOWN://下
                    {
                        if(change_flag && cnt_flag!=3&&cnt_flag!=7&&cnt_flag!=11)
                        {
                            unsigned short temp = num_str[cnt_flag].unicode();
                            if(num_str[cnt_flag] == '0')
                                num_str[cnt_flag]='9';
                            else
                            {
                                temp--;
                                num_str[cnt_flag] = temp;
                            }
                        }
                        else if((!change_flag)&&(focusWidget() !=btnlist_ui.back()))
                             focusNextChild();
                        break;
                    }
                    case LEFT://左
                    {
                        if(change_flag)
                        {
                            if(cnt_flag>0)
                            cnt_flag--;
                        else
                            cnt_flag=14;
                        }
                        else
                             focusPreviousChild();
                       break;
                    }
                    case RIGHT://右
                    {
                        if(change_flag)
                        {
                            if(cnt_flag <14)
                                cnt_flag++;
                            else
                                cnt_flag=0;
                        }
                        else
                             focusNextChild();
                       break;
                    }
                    case INIT: //打开初始化界面
                    {
                        init_Show *show = new init_Show(this);
                        show->show();
                        this->hide();
                        break;
                    }
                    case BACK:
                    {
                        this->parentWidget()->show();
                        this->close();
                        break;
                    }
                    default:
                        break;
                    }
                 }
 //                else if (event->type()==QEvent::FocusIn)     //然后再判断控件的具体事件 (这里指获得焦点事件)
 //                          {
 //                              QPushButton *focus_button = (QPushButton *)watched;
 //                                      focus_button->setFixedSize(160,60);
 //                          }
 //                else if (event->type()==QEvent::FocusOut)    // 这里指 控件的失去焦点事件
 //                         {
 //                    QPushButton *focus_button = (QPushButton *)watched;
 //                            focus_button->setFixedSize(120,50);
 //                         }
                 }
            return QMainWindow::eventFilter(watched,event);//返回父类的事件过滤器

     }

